<template>
  <div>
    <ul class="list">
      <li v-for="(item, index) in resultData" :key="index" >
        <div class="title">
          <span class="classify">【<i class="iconfont icon--nav-zixun"></i>资讯】</span>
          <h5 v-html="item['title_wordsLight']" @click="toDetail(item.id)"></h5>
        </div>
        <p v-html="item['text_wordsLight']" :title="item['text']" class="content"></p>
        <p class="other">
          <span v-show="item.source&&item.source!==''">{{item.source}}</span>
          <span v-show="item.pubdate!==''">{{item.pubdate}}</span>
        </p>
      </li>
      <!-- no-data -->
      <div v-if="noData" class="no-data">
        <loading1 :emptyText="emptyText"/>
      </div>
    </ul>
    <el-pagination
      v-if="resultData.length>0"
      @current-change="handleCurrentChange"
      layout="prev, pager, next"
      :current-page.sync="opts['ipi']"
      :page-size="opts['ips']"
      :total="total"
      class="elpagination"
    ></el-pagination>
  </div>
</template>

<script>
import {thousands, wordsLight} from '@/utils/utils'
export default {
  data() {
    return {
      opts: {},
      sort: 1,
      resultData: [],
      total: 0,
      noData: false,
      emptyText: '',
      facetParams: {}, // 聚合过来的参数
    }
  },
  methods: {
    init(opts) {
      this.opts = JSON.parse(JSON.stringify(opts))
      // 
      if (this.opts.resetFacet) {
        this.facetParams = {}
      }
      this.search()
    },
    search() {
      this.resultData = []
      this.noData = true
      this.emptyText = 'loading'
      let params = {
        ipi: this.opts.ipi,
        ips: this.opts.ips,
        type: 3,
      }
      if (this.opts.sort !== -1) {
        params['order'] = this.opts.sort
      }
      // 聚合的参数
      if (Object.values(this.facetParams).length > 0) {
        let keys = Object.keys(this.facetParams)
        keys.forEach(v => {
          params[`${v}Filter`] = this.facetParams[v]
        })
      }
      // 判断是否有高级检索
      if (this.opts.advanced) {
        if (this.opts.accurateSearch !== '') {
          params['accurateSearch'] = this.opts.accurateSearch
        }
        if (this.opts.includeSearch !== '') {
          params['includeSearch'] = this.opts.includeSearch
        }
        if (this.opts.notIncludeSearch !== '') {
          params['notIncludeSearch'] = this.opts.notIncludeSearch
        }
      } else {
        if (this.opts.searchTxt !== '') {
          params['keyword'] = this.opts.searchTxt
        }
      }
      
      this.$get(this.$Url.policy.list, params).then((res) => {
        let data = res.data
        let result = data.result || []
        if (result.length <= 0) {
          this.emptyText = '无法找到你所查询的内容，换个关键词试试'
        } else {
          this.noData = false
        }
        // 配置高亮
        let settings = [
          {words: this.opts.searchTxt, color: '#FF8C00'}
        ]
        result.forEach(v => {
          v.title_wordsLight = wordsLight(v.title, settings)
          if (v.text && v.text !== '') {
            let lastStr = v.text[v.text.length-1]
            let reg = /[.|。|？]/g
            if (!reg.test(lastStr)) {
              v.text += '...'
            }
          } else {
            v.text = '暂无内容'
          }
          v.text_wordsLight = wordsLight(v.text, settings)
          v.pubdate = v.pubdate && moment(v.pubdate).format('YYYY-MM-DD HH:mm') || ''
          this.resultData.push({
            ...v,
            checked: false,
          })
        })
        let _total = data.total || 0
        this.total = _total > 10000 ? 10000 : _total
        this.$emit('resultTotal', thousands(_total))
        // 聚合
        if (this.opts.resetFacet) {
          let facetData = {
            pubdate: data.pubdate,
            source: data.source,
            keywords: data.keywords,
          }
          this.$Bus.$emit('facetData', facetData)
        }
      }).catch(err => {
        this.emptyText = '无法找到你所查询的内容，换个关键词试试'
      })
    },
    // 分页
    handleCurrentChange(val) {
      this.opts.ipi = val
      this.search()
    },
    // 在进入企业详情之前，先判断后台是否生成数据
    toDetail(id) {
      let url = this.$router.resolve({
        name: "ConsultDetail",
        query: {
          id
        }
      })
      window.open(url.href, '_blank')
    },
  },
  mounted() {
    this.$Bus.$on('facetParams', params => {
      this.facetParams = params
      this.$set(this.opts, 'ipi', 1)
      this.search()
    })
  },
  beforeDestroy() {
    this.$Bus.$off('facetParams')
  }
}
</script>

<style lang="scss" scoped>
.list {
  min-height: 1.3rem;
  li {
    position: relative;
    padding-bottom: .2rem;
    border-bottom: 1px solid $color36;
    .title {
      position: relative;
      line-height: .48rem;
      padding-left: 0;
      padding-right: .6rem;
      border-bottom: 0;
      .classify {
        display: inline-block;
        min-width: .85rem;
        margin-right: .05rem;
        font-size: .14rem;
        vertical-align: middle;
        background-image: linear-gradient(50deg, $color55, $color56);
        background-clip:text;
        color: transparent;
        i {
          display: inline-block;
          padding: 0 .04rem;
        }
      }
      h5 {
        display: inline-block;
        width: calc(100% - 1.5rem);
        // font-family: SourceHanSansCN-Medium;
        font-weight: normal;
        font-size: .18rem;
        vertical-align: middle;
        color: $color57;
        cursor: pointer;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        ::v-deep {
          em {
            color: $color120;
          }
        }
      }
    }
    .content {
      display: -webkit-box;
      max-height: .96rem;
      line-height: .24rem;
      margin: .1rem 0;
      font-size: .15rem;
      color: $color58;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 4;//控制行数
      overflow: hidden;
      ::v-deep {
        em {
          color: $color120;
        }
        a {
          color: $color58;
        }
      }
    }
    .other {
      position: relative;
      display: flex;
      align-items: center;
      width: 100%;
      padding-top: .04rem;
      span {
        position: relative;
        display: inline-block;
        line-height: .24rem;
        padding: 0 .06rem;
        margin-right: .1rem;
        font-size: .12rem;
        &:nth-of-type(1) {
          color: $color66;
          background-color: $color67;
        }
        &:nth-of-type(2) {
          color: $color64;
          background-color: $color65;
        }
      }
    }
    .collect-btn {
      position: absolute;
      top: .14rem;
      right: 0;
      display: block;
      font-size: .16rem;
      line-height: .2rem;
      text-align: center;
      color: $color120;
      cursor: pointer;
      span {
        display: inline-block;
        vertical-align: middle;
        &.iconfont {
          font-size: .24rem;
        }
        &.label {
          margin-left: .05rem;
          font-size: .16rem;
        }
      }
      &.score {
        background-color: #8696E8;
      }
      &.el-icon-star-off {
        font-size: 18px;
        background: #909399;
        cursor: pointer;
      }
      &.active {
        font-size: 21px;
        background-color: #8696E8;
      }
    }
  }
}
.no-data {
  line-height: 1.2rem;
}
.elpagination {
  padding: .2rem 0;
  ::v-deep {
    .el-pager {
      li {
        font-weight: normal;
        &.active {
          font-size: 16px;
          font-weight: bold;
          color: $mainColor;
        }
      }
    }
    .more + .number {
      display: none;
    }
  }
}
</style>